/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sirtet.tetris.generator;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author yunglu
 */
public class RowPosition {

    public RowPosition(int r, int s,
            RowPosition upper, RowPosition left) {
        row = r;
        shift = s;
        parent = upper;
        if (upper != null) {
            if (upper.child == null) {
                upper.child = this;
            }
        }
        if (left != null) {
            left.sibling = this;
        }
    }

    public int getRow() {
        return row;
    }

    public int getShift() {
        return shift;
    }

    public RowPosition getSibling() {
        return sibling;
    }

    private boolean[][] fillMatrix(final List<Integer> rowLength) {
        int r = row; // should be rowLength.size() - 1
        int minShift = shift; // > 0 means the first few columns are empty
        int maxColumn = shift + rowLength.get(r).intValue();
        r--;
        RowPosition p = parent;
        // determine the number of columns        
        while ((r >= 0) && (p != null)) {
            if (minShift > p.shift) {
                minShift = p.shift;
            }
            int col = p.shift + rowLength.get(r).intValue();
            if (maxColumn < col) {
                maxColumn = col;
            }
            p = p.parent;
            r--;
        }
        // create matrix and initialize elements
        int col = maxColumn - minShift;
        boolean[][] matrix = new boolean[row + 1][col];
        for (r = 0; r < row + 1; r++) {
            for (int c = 0; c < col; c++) {
                matrix[r][c] = false;
            }
        }
        // fill in the squares
        p = this;
        r = row;
        while ((r >= 0) && (p != null)) {
            int rl = rowLength.get(r).intValue();
            int s = p.shift;
            for (int c = 0; c < rl; c++) {
                matrix[r][s + c - minShift] = true;
            }
            p = p.parent;
            r--;
        }
        return matrix;
    }

    private void createMatrix(final List<Integer> rowLength,
            List<boolean[][]> matrixList) {
        if (row == (rowLength.size() - 1)) {
            // last row, create a matrix            
            boolean[][] pieceMatrix = fillMatrix(rowLength);
            matrixList.add(pieceMatrix);
        }
        if (sibling != null) {
            sibling.createMatrix(rowLength, matrixList);
        }
        if (child != null) {
            child.createMatrix(rowLength, matrixList);
        }
    }

    public List<boolean[][]> createMatrixList(final List<Integer> rowLength) {
        List<boolean[][]> matrixList = new ArrayList<boolean[][]>();
        if (sibling != null) {
            sibling.createMatrix(rowLength, matrixList);
        }
        if (child != null) {
            child.createMatrix(rowLength, matrixList);
        }
        return matrixList;
    }
    int row;
    int shift;
    RowPosition sibling = null;
    RowPosition child = null;
    RowPosition parent = null;
}
